from sqlalchemy import Column, Integer, String, Float, ForeignKey, DateTime, Text, Boolean
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from app.database import Base


class Score(Base):
    __tablename__ = "scores"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    exam_id = Column(Integer, ForeignKey("exams.id"), nullable=False)
    score = Column(Float, nullable=False)  # 总分
    correct_count = Column(Integer, default=0)  # 正确题数
    wrong_count = Column(Integer, default=0)  # 错误题数
    total_count = Column(Integer, default=0)  # 总题数
    status = Column(String(20), default="completed")  # completed, cheating, absent
    start_time = Column(DateTime(timezone=True))  # 开始考试时间
    end_time = Column(DateTime(timezone=True))  # 交卷时间
    duration = Column(Integer)  # 实际考试用时（秒）
    ip_address = Column(String(45))  # 考试IP地址
    user_agent = Column(String(500))  # 浏览器信息
    submitted_at = Column(DateTime(timezone=True), server_default=func.now())
    remark = Column(Text)  # 备注信息
    
    # 关系
    user = relationship("User", back_populates="scores")
    exam = relationship("Exam", back_populates="scores")